import { type RouteRecordRaw, createRouter, createWebHashHistory, createWebHistory } from "vue-router"

const Layout = () => import("@/layout/index.vue")
export const admin = "超级管理员"

/** 常驻路由 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: {
      hidden: true
    },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/views/error-page/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/register",
    component: () => import("@/views/register/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/password",
    component: () => import("@/views/password/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/productDetil",
    component: () => import("@/views/productH5/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/productWebDetil",
    component: () => import("@/views/productH5/webH5.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  }
  // {
  //   path: "/oneself",
  //   component: () => import("@/views/oneself/index.vue"),
  //   meta: {
  //     hidden: true
  //   }
  // },
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const asyncRoutes: RouteRecordRaw[] = [
  // 权限
  {
    path: "/order",
    component: Layout,
    redirect: "/order/order-list",
    name: "order",
    meta: {
      title: "订单管理",
      elIcon: "Share",
      roles: [admin], // 可以在根路由中设置角色
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "OrderEntry",
        component: () => import("@/views/order/OrderEntry/index.vue"),
        name: "OrderEntry",
        meta: {
          title: "订单录入",
          // keepAlive: true,
          roles: [admin]
        }
      },

      {
        path: "belowOrder",
        component: () => import("@/views/order/belowOrder/index.vue"),
        name: "belowOrder",
        meta: {
          title: "下级订单",
          // keepAlive: true,
          roles: [admin]
        }
      }
    ]
  },
  {
    path: "/product",
    component: Layout,
    redirect: "/product/product-list",
    name: "product",
    meta: {
      title: "号卡业务",
      elIcon: "Shop",
      roles: [admin], // 可以在根路由中设置角色
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "product-list",
        component: () => import("@/views/order/productList/index1.vue"),
        name: "productList",
        meta: {
          title: "套餐列表",
          // keepAlive: true,
          roles: [admin]
        }
      },
      {
        path: "combo-compilation",
        component: () => import("@/views/order/productList/compilation.vue"),
        name: "Compilation",
        meta: {
          title: "套餐合集",
          keepAlive: true,
          hidden: false,
          roles: [admin]
        }
      },
      {
        path: "combo-h5-list",
        component: () => import("@/views/combo-h5/index.vue"),
        name: "ComboH5List",
        meta: {
          title: "H5套餐",
          keepAlive: true,
          hidden: true,
          roles: [admin]
        }
      },
      {
        path: "product-share",
        component: () => import("@/views/order/productList/index.vue"),
        name: "ProductShare",
        meta: {
          title: "H5套餐分享",
          keepAlive: true,
          hidden: true,
          roles: [admin]
        }
      },
      {
        path: "order-list-h5",
        component: () => import("@/views/order/orderH5/index.vue"),
        name: "OrderListH5",
        meta: {
          title: "H5订单",
          keepAlive: true,
          hidden: true,
          roles: [admin]
        }
      },

      {
        path: "childset",
        component: () => import("@/views/order/productList/childSet.vue"),
        name: "childset",
        meta: {
          title: "套餐合集-子套餐",
          keepAlive: false,
          hidden: true,
          roles: [admin]
        }
      }
    ]
  },
  {
    path: "/webset-manage",
    redirect: "/webset-manage/order-list",
    name: "WebSetManage",
    component: Layout,
    meta: {
      title: "宽带业务",
      // svgIcon: "lock",
      elIcon: "SetUp",
      roles: [admin], // 可以在根路由中设置角色
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "product-set",
        component: () => import("@/views/webset/product.vue"),
        name: "WebProductSet",
        meta: {
          title: "宽带产品",
          keepAlive: true,
          roles: [admin]
        }
      },
      {
        path: "combo-h5-web",
        component: () => import("@/views/webH5/combo.vue"),
        name: "ComboH5Web",
        meta: {
          title: "宽带H5套餐",
          keepAlive: true,
          roles: [admin],
          hidden: true
        }
      },
      {
        path: "webH5-share",
        component: () => import("@/views/webH5/share.vue"),
        name: "WebHtShare",
        meta: {
          title: "宽带H5套餐分享",
          keepAlive: true,
          roles: [admin],
          hidden: true
        }
      },
      {
        path: "order-list",
        component: () => import("@/views/webset/index.vue"),
        name: "WebList",
        meta: {
          title: "宽带订单列表",
          keepAlive: true,
          roles: [admin]
        }
      }
    ]
  },
  //H5相关
  {
    path: "/h5-manage",
    redirect: "/h5-manage/order-list-h5",
    name: "H5Manage",
    component: Layout,
    meta: {
      title: "号卡H5",
      elIcon: "Cellphone",
      roles: [admin],
      alwaysShow: true,
      hidden: true
    },
    children: []
  },
  {
    path: "/h5-manage-web",
    redirect: "/h5-manage-web/combo-h5-web",
    name: "H5ManageWeb",
    component: Layout,
    meta: {
      title: "宽带H5",
      elIcon: "Platform",
      roles: [admin],
      alwaysShow: true,
      hidden: true
    },
    children: [
      // {
      //   path: "order-web-h5",
      //   component: () => import("@/views/webH5/index.vue"),
      //   name: "OrderWebH5",
      //   meta: {
      //     title: "H5订单",
      //     keepAlive: true,
      //     roles: [admin]
      //   }
      // },
    ]
  },

  {
    path: "/co",
    component: Layout,
    redirect: "/co/",
    name: "co",
    meta: {
      title: "结佣模块",
      elIcon: "Pointer",
      roles: [admin], // 可以在根路由中设置角色
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "orderCenter",
        component: () => import("@/views/co/orderCenter/index.vue"),
        name: "orderCenter",
        meta: {
          title: "自推佣金",
          // keepAlive: true,
          roles: [admin]
        }
      },
      {
        path: "award",
        component: () => import("@/views/co/award/index.vue"),
        name: "award",
        meta: {
          title: "推广佣金",
          // keepAlive: true,
          roles: [admin]
        }
      },
      {
        path: "upup",
        component: () => import("@/views/co/upup/index.vue"),
        name: "upup",
        meta: {
          title: "激励奖金",
          // keepAlive: true,
          roles: [admin]
        }
      }
    ]
  },
  //   //存量业务相关
  {
    path: "/stock-manage",
    redirect: "/stock-manage/order",
    name: "StockManage",
    component: Layout,
    meta: {
      title: "存量业务",
      elIcon: "Histogram",
      roles: [admin],
      alwaysShow: true,
      hidden: true
    },
    children: [
      {
        path: "order",
        component: () => import("@/views/stock-operation/order.vue"),
        name: "OrderStockH5",
        meta: {
          title: "存量订单",
          keepAlive: true,
          roles: [admin]
        }
      },
      {
        path: "combo-set",
        component: () => import("@/views/stock-operation/comboSet.vue"),
        name: "ComboStockList",
        meta: {
          title: "存量套餐",
          keepAlive: true,
          roles: [admin]
        }
      }
    ]
  },
  {
    path: "/subordinate",
    redirect: "/subordinate/subordinate-list",
    name: "suBordinate",
    component: Layout,
    meta: {
      title: "下级代理",
      // svgIcon: "lock",
      elIcon: "UserFilled",
      roles: [admin], // 可以在根路由中设置角色
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "subordinate-list",
        component: () => import("@/views/subordinate/index.vue"),
        name: "subordinateList",
        meta: {
          title: "下级账号",
          keepAlive: true,
          roles: [admin]
        }
      }
    ]
  },
  // 权限
  {
    path: "/role",
    component: Layout,
    redirect: "/role/role-list",
    name: "Role",
    meta: {
      title: "权限管理",
      svgIcon: "lock",
      roles: [admin], // 可以在根路由中设置角色
      hidden: true,
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "role-list",
        component: () => import("@/views/role/role-list/index.vue"),
        name: "RoleList",
        meta: {
          title: "权限列表",
          // keepAlive: true,
          roles: [admin]
        }
      }
    ]
  },
  {
    path: "/:pathMatch(.*)*", // Must put the 'ErrorPage' route at the end, 必须将 'ErrorPage' 路由放在最后
    redirect: "/404",
    name: "ErrorPage",
    meta: {
      hidden: true
    }
  },
  {
    path: "/oneself",
    component: Layout,
    redirect: "/oneself",

    // meta: { hidden: true }, <el-icon><Tools /></el-icon>
    children: [
      {
        path: "/oneself",
        component: () => import("@/views/oneself/index.vue"),

        name: "oneself",
        meta: {
          title: "个人中心",
          elIcon: "Tools"
        }
      }
    ]
  }
]

const router = createRouter({
  history:
    import.meta.env.VITE_ROUTER_HISTORY === "hash"
      ? createWebHashHistory(import.meta.env.VITE_PUBLIC_PATH)
      : createWebHistory(import.meta.env.VITE_PUBLIC_PATH),
  routes: constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch (error) {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
